package com.jd.product.service.impl;

import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.product.domain.Product;
import com.jd.product.domain.ProductDetail;
import com.jd.product.mapper.ProductDetailMapper;
import com.jd.product.mapper.ProductMapper;
import com.jd.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Override
    @Transactional
    public void update(Product product) {
        super.update(product);
        // 获取服务详情
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail!=null){
            productDetail.setProductId(product.getId());
            productDetailMapper.update(productDetail);
        }

    }

    @Override
    @Transactional
    public void save(Product product) {
        super.save(product);
        //先获取宠物详情
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail!=null){
            productDetail.setProductId(product.getId());
            productDetailMapper.save(productDetail);
        }
    }

    @Override
    @Transactional
    public void remove(Long id) {
        super.remove(id);
        productDetailMapper.removeByProductId(id);
    }

    @Override
    @Transactional
    public void patchRemove(List<Long> ids) {
        super.patchRemove(ids);
        //删除宠物详情
        productDetailMapper.patchRemoveByProductId(ids);
    }
}
